package gui.other;

import enums.PremisaTypeEnum;
import j_util.Utiles;
import model.Premisa;
import model.Sylogism;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
/*

    This file is part of Logica-Logike.

    Logica-Logike is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation version 3 of the License.

    Logica-Logike is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Logica-Logike.  If not, see <http://www.gnu.org/licenses/>.

 */
/**
 * @author Jose Martin Rozanec;
 */
public class SylogismPremisaViewerDialog extends JDialog {
    private JList list;
    private JButton next;
    private JButton previous;
    private java.util.List<Sylogism> sylogismList;
    private int index;
    private boolean accepted;
    private DefaultListModel model;

    public SylogismPremisaViewerDialog(java.util.List<Sylogism> sylogismList) {
        this.sylogismList = sylogismList;
        index = 0;
        accepted = false;
        initComponents();
        if (sylogismList.size() != 0) {
            setSylogismStringModel();
        }
        manageButtons();
    }

    private void initComponents() {
        setIconImage(Utiles.createImageIcon("/gui/images/other/logicaIcon.png").getImage());
        setLayout(new GridBagLayout());
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.gridwidth = 6;
        constraints.gridheight = 4;
        list = new JList();
        model = new DefaultListModel();
        list.setModel(model);
        list.setPreferredSize(new Dimension(335, 60));
        add(list, constraints);

        constraints.insets = new Insets(20, 0, 0, 0);
        constraints.gridwidth = 1;
        constraints.gridheight = 1;
        constraints.gridy = 5;
        constraints.gridx = 1;
        previous = new JButton("Anterior");
        previous.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                index--;
                setSylogismStringModel();
                manageButtons();
            }
        });
        previous.setEnabled(false);
        add(previous, constraints);

        constraints.gridy = 5;
        constraints.gridx = 2;
        next = new JButton("Siguiente");
        next.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                index++;
                setSylogismStringModel();
                manageButtons();
                validate();
            }
        });
        if (sylogismList.size() == 0) {
            next.setEnabled(false);
        }
        add(next, constraints);

        constraints.insets = new Insets(0, 0, 0, 0);
        constraints.gridy = 6;
        constraints.gridx = 3;
        JButton accept = new JButton("Aceptar");
        accept.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                accepted = true;
                dispose();
            }
        });

        add(accept, constraints);

        constraints.gridy = 6;
        constraints.gridx = 4;
        JButton cancel = new JButton("Cancelar");
        cancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        add(cancel, constraints);
        getContentPane().setBackground(Color.WHITE);
    }

    private void manageButtons() {
        if (index == 0) {
            previous.setEnabled(false);
        } else {
            previous.setEnabled(true);
        }
        if (index == (sylogismList.size() - 1) || (sylogismList.size() == 0)) {
            next.setEnabled(false);
        } else {
            next.setEnabled(true);
        }
        validate();
    }

    public boolean accepted() {
        return accepted;
    }

    public Premisa getPremisa() {
        return new Premisa(list.getSelectedValue().toString());
    }

    public void setSylogismStringModel() {
        model.removeAllElements();
        model.addElement(sylogismList.get(index).getPremisa(PremisaTypeEnum.FIRST).toString());
        model.addElement(sylogismList.get(index).getPremisa(PremisaTypeEnum.SECOND).toString());
        model.addElement(sylogismList.get(index).getPremisa(PremisaTypeEnum.CONCLUSION).toString());
        list.setSelectedIndex(0);
    }

    public static void main(String[] args) {
        java.util.List<Sylogism> list = new ArrayList<Sylogism>();
        Sylogism sylogism = new Sylogism();
        sylogism.setPremisa(new Premisa("Todo h es m"), PremisaTypeEnum.FIRST);
        sylogism.setPremisa(new Premisa("Todo m es p"), PremisaTypeEnum.SECOND);
        sylogism.setPremisa(new Premisa("Todo p es h"), PremisaTypeEnum.CONCLUSION);
        list.add(sylogism);
        SylogismPremisaViewerDialog sylogismPremisaViewerDialog =
                new SylogismPremisaViewerDialog(list);
        sylogismPremisaViewerDialog.setPreferredSize(new Dimension(450, 200));
        sylogismPremisaViewerDialog.setSize(new Dimension(400, 200));
        sylogismPremisaViewerDialog.setModal(true);
        sylogismPremisaViewerDialog.setVisible(true);
        System.exit(0);
    }
}
